home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / ZODB / ConflictResolution.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  4.2 KB  |  142 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import logging
  5. from cStringIO import StringIO
  6. from cPickle import Unpickler, Pickler
  7. from pickle import PicklingError
  8. from ZODB.POSException import ConflictError
  9. from ZODB.loglevels import BLATHER
  10. logger = logging.getLogger('ZODB.ConflictResolution')
  11. ResolvedSerial = 'rs'
  12.  
  13. class BadClassName(Exception):
  14.     pass
  15.  
  16. _class_cache = { }
  17. _class_cache_get = _class_cache.get
  18.  
  19. def find_global(*args):
  20.     cls = _class_cache_get(args, 0)
  21.     if cls == 0:
  22.         
  23.         try:
  24.             module = __import__(args[0], { }, { }, [
  25.                 'cluck'])
  26.         except ImportError:
  27.             cls = 1
  28.  
  29.         cls = getattr(module, args[1], 1)
  30.         _class_cache[args] = cls
  31.         if cls == 1:
  32.             logger.log(BLATHER, 'Unable to load class', exc_info = True)
  33.         
  34.     
  35.     if cls == 1:
  36.         raise BadClassName(*args)
  37.     
  38.     return cls
  39.  
  40.  
  41. def state(self, oid, serial, prfactory, p = ''):
  42.     if not p:
  43.         pass
  44.     p = self.loadSerial(oid, serial)
  45.     file = StringIO(p)
  46.     unpickler = Unpickler(file)
  47.     unpickler.find_global = find_global
  48.     unpickler.persistent_load = prfactory.persistent_load
  49.     unpickler.load()
  50.     return unpickler.load()
  51.  
  52.  
  53. class PersistentReference:
  54.     
  55.     def __repr__(self):
  56.         return 'PR(%s %s)' % (id(self), self.data)
  57.  
  58.     
  59.     def __getstate__(self):
  60.         raise PicklingError("Can't pickle PersistentReference")
  61.  
  62.  
  63.  
  64. class PersistentReferenceFactory:
  65.     data = None
  66.     
  67.     def persistent_load(self, oid):
  68.         if self.data is None:
  69.             self.data = { }
  70.         
  71.         r = self.data.get(oid, None)
  72.         if r is None:
  73.             r = PersistentReference()
  74.             r.data = oid
  75.             self.data[oid] = r
  76.         
  77.         return r
  78.  
  79.  
  80.  
  81. def persistent_id(object):
  82.     if getattr(object, '__class__', 0) is not PersistentReference:
  83.         return None
  84.     
  85.     return object.data
  86.  
  87. _unresolvable = { }
  88.  
  89. def tryToResolveConflict(self, oid, committedSerial, oldSerial, newpickle, committedData = ''):
  90.     
  91.     try:
  92.         prfactory = PersistentReferenceFactory()
  93.         file = StringIO(newpickle)
  94.         unpickler = Unpickler(file)
  95.         unpickler.find_global = find_global
  96.         unpickler.persistent_load = prfactory.persistent_load
  97.         meta = unpickler.load()
  98.         if isinstance(meta, tuple):
  99.             klass = meta[0]
  100.             if not meta[1]:
  101.                 pass
  102.             newargs = ()
  103.             if isinstance(klass, tuple):
  104.                 klass = find_global(*klass)
  105.             
  106.         else:
  107.             klass = meta
  108.             newargs = ()
  109.         if klass in _unresolvable:
  110.             return None
  111.         
  112.         newstate = unpickler.load()
  113.         inst = klass.__new__(klass, *newargs)
  114.         
  115.         try:
  116.             resolve = inst._p_resolveConflict
  117.         except AttributeError:
  118.             _unresolvable[klass] = 1
  119.             return None
  120.  
  121.         old = state(self, oid, oldSerial, prfactory)
  122.         committed = state(self, oid, committedSerial, prfactory, committedData)
  123.         resolved = resolve(old, committed, newstate)
  124.         file = StringIO()
  125.         pickler = Pickler(file, 1)
  126.         pickler.persistent_id = persistent_id
  127.         pickler.dump(meta)
  128.         pickler.dump(resolved)
  129.         return file.getvalue(1)
  130.     except (ConflictError, BadClassName):
  131.         return None
  132.     except:
  133.         logger.error('Unexpected error', exc_info = True)
  134.         return None
  135.  
  136.  
  137.  
  138. class ConflictResolvingStorage:
  139.     '''Mix-in class that provides conflict resolution handling for storages'''
  140.     tryToResolveConflict = tryToResolveConflict
  141.  
  142.